﻿using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FeedbackForm : Form
    {
        CourseBLL courseBLL = new CourseBLL();
        FeedbackBLL feedbackBLL = new FeedbackBLL();
        UserBLL userBLL = new UserBLL();
        string[] question;
        int dem = 0;
        string[,] answer = new string[2, 8];
        string selectedText, userName;

        public FeedbackForm()
        {
            InitializeComponent();
        }
        public FeedbackForm(string text) : this()
        {
            userName = text;
        }

        public string[] LoadQuestion()
        {
            try
            {
                string[] question = new string[10];
                StreamReader reader = File.OpenText(@"123.txt");
                for (int i = 0; i < 10; i++)
                {
                    string temp = reader.ReadLine();

                    question[i] = temp;
                }

                return question;
            }
            catch
            {
                throw new FileNotFoundException("Khong tim thay file");
            }


        }

        private void FeedbackForm_Load(object sender, EventArgs e)
        {
            //load question
           
            try
            {
                question = LoadQuestion();
                txtQuestion.Text = question[dem];

            }
            catch (FileNotFoundException ex)
            {
                MessageBox.Show(ex.Message);
                //ghi log
            }
            comboBox1.DataSource = courseBLL.GetAllCourse();
            comboBox1.DisplayMember = "CourseName";
            comboBox1.ValueMember = "CourseID";
            button3.Enabled = false;
            
            timer1.Start();




        }

        private void button2_Click(object sender, EventArgs e) //Next Button
        {
            if (dem + 1 >= 7) button3.Enabled = true;
            else button3.Enabled = false;
            try
            {
                SaveQuestion(dem);

                if (dem < 7)
                {

                    dem++;
                    txtQuestion.Text = question[dem];
                    SetCauHoi(dem);
                    clear();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button1_Click(object sender, EventArgs e) //Previous Button
        {
            try
            {                

                if (dem > 0)
                {
                    SaveQuestion(dem);
                    dem--;
                    txtQuestion.Text = question[dem];
                    SetCauHoi(dem);
                    clear();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        public void SaveQuestion(int dem)
        {
            if (IsChecked())
            {
                answer[0, dem] = selectedText;
                answer[1, dem] = txtFeedback.Text;
            }
            else
                throw new Exception("Vui long chon 1 cau tra loi!");

        }


        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
                selectedText = ((RadioButton)sender).Text;
        }



        private void button3_Click(object sender, EventArgs e) //Finish Button

        {
            try
            {
                SaveQuestion(dem);
                feedbackBLL.SaveFeedback(userName, comboBox1.SelectedValue.ToString(), answer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        void clear()
        {
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            radioButton3.Checked = false;
            radioButton4.Checked = false;
            radioButton5.Checked = false;

        }
        bool IsChecked()
        {
            bool flag = false;
            if (radioButton1.Checked == true)
            {
                flag = true;
                selectedText = 1 + "";
            }
            else
                if (radioButton2.Checked == true)
            {
                flag = true;
                selectedText = 2 + "";
            }
            else
                if (radioButton3.Checked == true)
            {
                flag = true;
                selectedText = 3 + "";
            }
            else
                if (radioButton4.Checked == true)
            {
                flag = true;
                selectedText = 4 + "";
            }
            else
                if (radioButton5.Checked == true)
            {
                flag = true;
                selectedText = 5 + "";
            }
            return flag;
        }
        void SetCauHoi(int dem)
        {
            string temp= (dem+1) + "/8";
            lblCauHoi.Text = temp;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetKhoaHoc();
        }

        private void FeedbackForm_FormClosed(object sender, FormClosedEventArgs e)
        {
           
                userBLL.OffUser(userName);
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            listBox1.DataSource = userBLL.GetUserOnline();
            listBox1.DisplayMember = "UserName";
        }

        void SetKhoaHoc()
        {
            dem = 0;
            txtQuestion.Text=question[dem];
            SetCauHoi(dem);
        }
    }
}
